package com.huan.demo.handler;

import com.huan.demo.message.ChatRequestMessage;
import com.huan.demo.message.ChatResponseMessage;
import com.huan.demo.message.LoginRequestMessage;
import com.huan.demo.message.LoginResponseMessage;
import com.huan.demo.server.service.UserServiceFactory;
import com.huan.demo.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        String msgTo = msg.getTo();
        String from = msg.getFrom();
        String content = msg.getContent();
        System.out.println("发送数据是："+content);
        Channel channel = SessionFactory.getSession().getChannel(msgTo);
        if (channel != null) {
            channel.writeAndFlush(new ChatResponseMessage(from,content));
        }else {
            channel.writeAndFlush(new ChatResponseMessage(false,"对方不在线"));
        }
    }
}
